import 'package:flutter/material.dart';
import 'package:zywz/constants/ChinaCities.dart';

/// 企业绑定弹窗
///
/// 用于绑定企业信息，包含企业名称、地区、纳税人识别号等信息
class EnterpriseBindingDialog extends StatefulWidget {
  /// 确认按钮回调
  final Function(Map<String, dynamic> data)? onConfirm;

  /// 取消按钮回调
  final VoidCallback? onCancel;
  final String? title;  // 明确声明为可空类型

  const EnterpriseBindingDialog({
    Key? key,
    this.title,
    this.onConfirm,
    this.onCancel,
  }) : super(key: key);

  /// 显示企业绑定弹窗
  static Future<Map<String, dynamic>?> show(BuildContext context,{String? title}) {
    return showDialog<Map<String, dynamic>>(
      context: context,
      builder: (context) => EnterpriseBindingDialog(
        title: title ?? '绑定企业',
        onConfirm: (data) {
          Navigator.of(context).pop(data);
        },
        onCancel: () {
          Navigator.of(context).pop();
        },
      ),
    );
  }

  @override
  State<EnterpriseBindingDialog> createState() => _EnterpriseBindingDialogState();
}

class _EnterpriseBindingDialogState extends State<EnterpriseBindingDialog> {
  // 表单控制器
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _taxIdController = TextEditingController();
  final TextEditingController _taxRegisterNoController = TextEditingController();
  final TextEditingController _taxRegisterPwdController = TextEditingController();
  final TextEditingController _phoneController = TextEditingController();

  // 省份和城市
  String? _selectedProvince;
  String? _selectedCity;



  @override
  void dispose() {
    _nameController.dispose();
    _taxIdController.dispose();
    _taxRegisterNoController.dispose();
    _taxRegisterPwdController.dispose();
    _phoneController.dispose();
    super.dispose();
  }

  // 构建输入框
  Widget _buildTextField(String hintText, TextEditingController controller) {
    return Container(
      height: 50,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey.shade300),
        borderRadius: BorderRadius.circular(4),
      ),
      child: TextField(
        controller: controller,
        decoration: InputDecoration(
          hintText: hintText,
          hintStyle: TextStyle(color: Colors.grey.shade400, fontSize: 14),
          contentPadding: const EdgeInsets.symmetric(horizontal: 12),
          border: InputBorder.none,
        ),
      ),
    );
  }

  // 构建下拉选择框
  Widget _buildDropdown(String hintText, String? value, List<String> items, Function(String?) onChanged) {
    return Container(
      height: 50,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey.shade300),
        borderRadius: BorderRadius.circular(4),
      ),
      child: DropdownButtonHideUnderline(
        child: DropdownButton<String>(
          value: value,
          hint: Padding(
            padding: const EdgeInsets.only(left: 12),
            child: Text(
              hintText,
              style: TextStyle(color: Colors.grey.shade400, fontSize: 14),
            ),
          ),
          isExpanded: true,
          icon: const Icon(Icons.keyboard_arrow_down),
          padding: const EdgeInsets.symmetric(horizontal: 12),
          items: items.map((String item) {
            return DropdownMenuItem<String>(
              value: item,
              child: Text(item),
            );
          }).toList(),
          onChanged: onChanged,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
        backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 标题
            Center(
              child: Text(
                widget.title ?? '绑定企业',
                style: const TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            const SizedBox(height: 20),

            // 企业名称
            const Text('企业名称'),
            const SizedBox(height: 8),
            _buildTextField('请输入', _nameController),
            const SizedBox(height: 16),

            // 地区
            const Text('地区'),
            const SizedBox(height: 8),
            Row(
              children: [
                Expanded(
                  child: _buildDropdown(
                    '省份',
                    _selectedProvince,
                    ChinaCities.provinces,
                        (value) {
                      setState(() {
                        _selectedProvince = value;
                        _selectedCity = null; // 重置城市选择
                      });
                    },
                  ),
                ),
                const SizedBox(width: 10),
                Expanded(
                  child: _buildDropdown(
                    '城市',
                    _selectedCity,
                    _selectedProvince != null ?  ChinaCities.cities[_selectedProvince]! : [],
                        (value) {
                      setState(() {
                        _selectedCity = value;
                      });
                    },
                  ),
                ),
              ],
            ),
            const SizedBox(height: 16),

            // 纳税人识别号
            const Text('纳税人识别号'),
            const SizedBox(height: 8),
            _buildTextField('请输入', _taxIdController),
            const SizedBox(height: 16),

            // 电子税务局登录账号
            const Text('电子税务局登录账号'),
            const SizedBox(height: 8),
            _buildTextField('请输入', _taxRegisterNoController),
            const SizedBox(height: 16),

            // 电子税务局登录密码
            const Text('电子税务局登录密码'),
            const SizedBox(height: 8),
            _buildTextField('请输入', _taxRegisterPwdController),
            const SizedBox(height: 16),

            // 对应手机号
            const Text('对应手机号'),
            const SizedBox(height: 8),
            _buildTextField('企业对应税务局绑定手机号', _phoneController),
            const SizedBox(height: 24),

            // 按钮
            Row(
              children: [
                Expanded(
                  child: ElevatedButton(
                    onPressed: () {
                      if (widget.onConfirm != null) {
                        widget.onConfirm!({
                          'name': _nameController.text,
                          'province': _selectedProvince,
                          'city': _selectedCity,
                          'taxCode': _taxIdController.text,
                          'nationalName': _taxRegisterNoController.text,
                          'nationalPwd': _taxRegisterPwdController.text,
                          'taxPayerPhone': _phoneController.text,
                        });
                      }

                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color(0xFF4DD0E1),
                      foregroundColor: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25),
                      ),
                      padding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                    child: const Text('确 定'),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: OutlinedButton(
                    onPressed: widget.onCancel,
                    style: OutlinedButton.styleFrom(
                      foregroundColor: Colors.black,
                      side: const BorderSide(color: Colors.transparent),
                      backgroundColor: const Color(0xFF4DD0E1).withOpacity(0.1),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25),
                      ),
                      padding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                    child: const Text('取 消'),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}